<?php
/**
 * Get product details tab.
 */
function get_product_tab(&$data, $node) {
  if (isset($node->field_product_tab_list) && !empty($node->field_product_tab_list)) {
    $field_product_tab_list = $node->field_product_tab_list[LANGUAGE_NONE];
    $i = 0;
    foreach ((array) $field_product_tab_list as $key => $value) {
      $item = field_collection_item_revision_load($value['revision_id']);
      get_product_tab_by_item($data, $item, $i);
      $i++;
    }
  }
  return $data;
}

/**
 * Get product details tab by item.
 */
function get_product_tab_by_item(&$data, $item, $i) {
  $data['product_tab_list'][$i]['field_product_tab_title'] = get_field_value('field_product_tab_title', $item);
  $data['product_tab_list'][$i]['field_product_tab_content'] = get_field_value('field_tab_content', $item, TRUE);
  $data['product_tab_list'][$i]['field_product_tab_video'] = get_field_value('field_video', $item) ? file_create_url(get_field_value('field_video', $item)) : '';
  $data['product_tab_list'][$i]['field_product_tab_down'] = get_field_value('field_product_tab_down', $item);
  $data['product_tab_list'][$i]['field_product_tab_image_show_type'] = get_field_value('field_image_show_type', $item);
  if (isset($item->field_product_tab_tab_list) && !empty($item->field_product_tab_tab_list)) {
    $field_product_tab_tab_list = $item->field_product_tab_tab_list[LANGUAGE_NONE];
    $b = 0;
    foreach ((array) $field_product_tab_tab_list as $key => $value) {
      $item = field_collection_item_revision_load($value['revision_id']);
      get_product_tab_tab_by_item($data, $item, $i, $b);
      $b++;
    }
  }
}

/**
 * Get product details tab by item.
 */
function get_product_tab_tab_by_item(&$data, $item, $i, $b) {
//  var_dump($item);exit;
  $data['product_tab_list'][$i]['product_tab_tab_list'][$b]['field_product_tab_tab_title'] = get_field_value('field_product_tab_title', $item);
  $data['product_tab_list'][$i]['product_tab_tab_list'][$b]['item_id'] = $item->item_id;
  $data['product_tab_list'][$i]['product_tab_tab_list'][$b]['field_product_tab_tab_content'] = get_field_value('field_tab_content', $item);
  $data['product_tab_list'][$i]['product_tab_tab_list'][$b]['field_product_tab_tab_image_video'] = $item->field_image_video[LANGUAGE_NONE];
  $data['product_tab_list'][$i]['product_tab_tab_list'][$b]['field_product_tab_tab_file'] = get_field_value('field_file', $item) ? file_create_url(get_field_value('field_file', $item)) : '';
  $data['product_tab_list'][$i]['product_tab_tab_list'][$b]['field_product_tab_tab_filesize'] = get_field_value('field_file', $item) ? filesize(get_field_value('field_file', $item)) : '';
  if (isset($item->field_image_video) && !empty($item->field_image_video)) {
    $field_product_tab_tab_image_video = $item->field_image_video[LANGUAGE_NONE];
    $d = 0;
    foreach ((array) $field_product_tab_tab_image_video as $key => $value) {
      $item = field_collection_item_revision_load($value['revision_id']);
//      var_dump($item);exit;
//      var_dump(field_info_instance('field_collection_item', 'field_image', 'product'));exit;
//      var_dump(field_get_default_value('field_collection_item',$item,field_info_field('field_image'),field_info_instance('field_collection_item', 'field_image', 'product')));exit;
      $data['product_tab_list'][$i]['product_tab_tab_list'][$b]['field_product_tab_tab_image_video'][$d]['content'] = get_field_value('field_tab_content', $item);
      $data['product_tab_list'][$i]['product_tab_tab_list'][$b]['field_product_tab_tab_image_video'][$d]['url'] = get_field_value('field_file', $item) ? file_create_url(get_field_value('field_file', $item)) : '';
      $data['product_tab_list'][$i]['product_tab_tab_list'][$b]['field_product_tab_tab_image_video'][$d]['thumb'] = get_field_value('field_image', $item) ? file_create_url(get_field_value('field_image', $item)) : '';
      $d++;
    }
  }
}

/**
 * Get product references.
 */
function get_product_references(&$data, $node) {
  if (isset($node->field_product_references) && !empty($node->field_product_references)) {
    $field_product_references = $node->field_product_references[LANGUAGE_NONE];
    $i = 0;
    foreach ((array) $field_product_references as $key => $value) {
      $item = field_collection_item_revision_load($value['revision_id']);
      $data['field_product_references'][$i]['field_product_references_content'] = get_field_value('field_product_references_content', $item);
      $data['field_product_references'][$i]['field_product_references_url'] = get_field_value('field_product_references_url', $item);
      $i++;
    }
  }
  return $data;
}

